package cn.demoncat.util.webmagic;

import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.pipeline.Pipeline;
import us.codecraft.webmagic.processor.PageProcessor;

import java.util.concurrent.ExecutorService;

/**
 * WebMagic工具
 *
 * @author 延晓磊
 * @since 2022年09月01日
 */
public class WebMagicUtil {


    /**
     * 启动爬虫
     *
     * @param url               初始URL
     * @param processor         页面解析器
     * @param pipeline          结果处理器：默认打印到控制台
     * @param executorService   线程池：默认根据threadNum创建
     * @param threadNum         线程数：默认1
     *
     */
    public static void run(String url, PageProcessor processor, Pipeline pipeline, ExecutorService executorService, int threadNum){
        build(url, processor, pipeline, executorService, threadNum).run();
    }

    /**
     * 启动爬虫
     *
     * @param url               初始URL
     * @param processor         页面解析器
     * @param pipeline          结果处理器：默认打印到控制台
     * @param threadNum         线程数：默认1
     *
     */
    public static void run(String url, PageProcessor processor, Pipeline pipeline, int threadNum){
        build(url, processor, pipeline, null, threadNum).run();
    }

    /**
     * 启动爬虫（异步）
     *
     * @param url               初始URL
     * @param processor         页面解析器
     * @param pipeline          结果处理器：默认打印到控制台
     * @param executorService   线程池：默认根据threadNum创建
     * @param threadNum         线程数：默认1
     *
     */
    public static void runAsync(String url, PageProcessor processor, Pipeline pipeline, ExecutorService executorService, int threadNum){
        build(url, processor, pipeline, executorService, threadNum).runAsync();
    }

    /**
     * 启动爬虫（异步）
     *
     * @param url               初始URL
     * @param processor         页面解析器
     * @param pipeline          结果处理器：默认打印到控制台
     * @param threadNum         线程数：默认1
     *
     */
    public static void runAsync(String url, PageProcessor processor, Pipeline pipeline, int threadNum){
        build(url, processor, pipeline, null, threadNum).runAsync();
    }

    /**
     * 构建爬虫
     *
     * @param url               初始URL
     * @param processor         页面解析器
     * @param pipeline          结果处理器：默认打印到控制台
     * @param executorService   线程池：默认根据threadNum创建
     * @param threadNum         线程数：默认1
     *
     * @author 延晓磊
     * @since 2022年09月01日
     */
    private static Spider build(String url, PageProcessor processor, Pipeline pipeline, ExecutorService executorService, int threadNum){
        // 创建爬虫
        Spider spider = Spider.create(processor).addUrl(url);
        // 结果处理
        if (pipeline != null) {
            spider.addPipeline(pipeline);
        }
        // 多线程
        if (threadNum > 0) {
            if (executorService == null) {
                spider.thread(threadNum);
            }else{
                spider.thread(executorService, threadNum);
            }
        }
        return spider;
    }

}
